package com.example.love_margin.Service.Impl;


import com.example.love_margin.Entity.Search;
import com.example.love_margin.Mapper.SearchMapper;
import com.example.love_margin.Service.Impl.Until.delPetsUtil;
import com.example.love_margin.Service.SearchService;
import com.example.love_margin.Untils.ResultCode;
import com.example.love_margin.Untils.forDelete;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SearchServiceImpl implements SearchService {


    @Autowired
    SearchMapper searchMapper;


    //创建删除批量文件的实例化
    @Autowired
    forDelete delete = new forDelete();
    @Autowired
    delPetsUtil delP = new delPetsUtil();

    @Override
    public Object addSearchPet(Search search) {
        if(searchMapper.addSearch(search)==1){
            return ResultCode.SUCCESS.result("成功");
        }
        return ResultCode.SUCCESS.result("失败");
    }

    @Override
    public Object deleteSearchPet(int search_id) {
        if(searchMapper.deleteSearch(search_id)==1){
            return ResultCode.SUCCESS.result("删除成功");
        }
        return ResultCode.SUCCESS.result("删除失败");
    }

    @Override
    public Object deleteSearchPets(Integer[] search_ids) {

        String URLs = delP.admin("search_pet_picture","search","search_id",search_ids);
        String sql = delP.deleSQL("search","search_id",search_ids);
        List<String> realUrls = searchMapper.selectPetAll(URLs);
        if(delete.DeleteList(realUrls)) {
            if (searchMapper.deleteScienceM(sql) >= 1) {
                return ResultCode.SUCCESS.result("删除成功");
            } else {
                return ResultCode.SUCCESS.result("删除失败");
            }
        }
        return ResultCode.SUCCESS.result("删除失败");
    }

    @Override
    public Object passMySearch(int search_id) {
        if(searchMapper.passSearch("已找到",search_id)==1){
            return "通过成功";
        }
        return "通过失败";
    }

    @Override
    public Object updateSearchPet(Search search) {
        return null;
    }

    @Override
    public Object selectOneSearchPet(int search_id) {
        return ResultCode.SUCCESS.result(searchMapper.selectOneSearch(search_id));
    }



    @Override
    public Object selectSearchPetPage(String search_pet_class,int user_id) {
        return ResultCode.SUCCESS.result(searchMapper.selectMySearch(search_pet_class,user_id));
    }

    @Override
    public List<Search> selectSearchMany(String search_pet_class,String search_pet_category, String search_pet_varieties, String key, Integer pageNum, Integer pageSize) {
        //用插件进行分页
        PageHelper.startPage(pageNum, pageSize);
        return searchMapper.selectSearchMany(search_pet_class,search_pet_category,search_pet_varieties,key);
    }
}
